#include <bsl_err_internal.h>
#include <bsl_params.h>
#include <crypt_algid.h>
#include <crypt_eal_implprovider.h>
#include <crypt_eal_provider.h>
#include <crypt_errno.h>

#include "kaev1_digests.h"

static void *KAE_DIGEST_DefaultNewCtx(void *provCtx, int32_t algId)
{
    (void)provCtx;
    void *mdCtx = NULL;
    switch (algId) {
        case CRYPT_MD_MD5:
            mdCtx = KAE_DIGEST_NewCtx(CRYPT_MD_MD5);
            break;
        case CRYPT_MD_SM3:
            mdCtx = KAE_DIGEST_NewCtx(CRYPT_MD_SM3);
            break;
        case CRYPT_MD_SHA1:
            mdCtx = KAE_DIGEST_NewCtx(CRYPT_MD_SHA1);
            break;
        case CRYPT_MD_SHA256:
            mdCtx = KAE_DIGEST_NewCtx(CRYPT_MD_SHA256);
            break;
        case CRYPT_MD_SHA224:
            mdCtx = KAE_DIGEST_NewCtx(CRYPT_MD_SHA224);
            break;
        case CRYPT_MD_SHA384:
            mdCtx = KAE_DIGEST_NewCtx(CRYPT_MD_SHA384);
            break;
        case CRYPT_MD_SHA512:
            mdCtx = KAE_DIGEST_NewCtx(CRYPT_MD_SHA512);
            break;
    }
    if (mdCtx == NULL) {
        BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT);
        return NULL;
    }
    return mdCtx;
}

int32_t KAE_DIGEST_DefaultCtrl(void *ctx, int32_t cmd, void *val, uint32_t valLen)
{
    (void)ctx;
    (void)cmd;
    (void)val;
    (void)valLen;
    return CRYPT_NOT_SUPPORT;
}

const CRYPT_EAL_Func kaeMd[] = {
    {CRYPT_EAL_IMPLMD_NEWCTX, KAE_DIGEST_DefaultNewCtx},
    {CRYPT_EAL_IMPLMD_INITCTX, KAE_DIGEST_Init},
    {CRYPT_EAL_IMPLMD_UPDATE, KAE_DIGEST_Update},
    {CRYPT_EAL_IMPLMD_FINAL, KAE_DIGEST_Final},
    {CRYPT_EAL_IMPLMD_DEINITCTX, KAE_DIGEST_Deinit},
    {CRYPT_EAL_IMPLMD_DUPCTX, KAE_DIGEST_Dup},
    {CRYPT_EAL_IMPLMD_CTRL, KAE_DIGEST_DefaultCtrl},
    {CRYPT_EAL_IMPLMD_FREECTX, KAE_DIGEST_Free},
    CRYPT_EAL_FUNC_END,
};
